<!-- 注册
 1. vue始终在用双向绑定的思想来准备前后端交互，每一个vue都会定义好变量给 表单（form）或者表格（table）中的html元素的值绑定
-->
<template>
  <div><!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
    <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
    <el-dialog title="标题名" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">

      <el-form :model="adminrno" label-width="120px" :rules="addAdminrnoRules" ref="addAdminrnoForm">
        <el-form-item label="餐厅状态" prop="rastate">
          <el-col :span="8">
            <el-input v-model="adminrno.rastate"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="开始日期">
          <el-col :span="8">
            <el-date-picker type="date" placeholder="选择日期" v-model="adminrno.radate"
                            style="width: 250px;"></el-date-picker>
          </el-col>
          <el-col class="line" :span="1">-</el-col>
          <el-col :span="8">
            <el-time-picker placeholder="选择时间" v-model="adminrno.ratime"
                            style="width: 200px;"></el-time-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="操作管理员">
          <el-col :span="5">
            <el-select v-model="adminrno.ano" placeholder="请选择">
              <el-option v-for="(admin,index) in admins"
                         :key="admin.aname"
                         :label="admin.aname"
                         :value="admin.ano"/>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="饭店">
          <el-col :span="5">
            <el-select v-model="adminrno.rno" placeholder="请选择">
              <el-option v-for="(restaurant,index) in restaurants"
                         :key="restaurant.rname"
                         :label="restaurant.rname"
                         :value="restaurant.rno"/>
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <span>
      <el-button type="success" @click="addAdminrno('addAdminrnoForm')">注册</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import api from "../../../../axios/api"
  import utils from "../../../../static/js/utils/upload.js"
  import rules from "../../../../static/js/validator/rules.js";

  export default {
    name: "addAdminrno",
    data() {
      return {
        adminrno: {rastate:"",ano:"",rno:"",ratime:"",radate:""},
        addDialogIsShow: false,
        admins: [{ano: "0001", aname: "小李"}, {ano: "0002", aname: "赵四"}, {ano: "0003", aname: "李涛"}],
        restaurants: [{rno: 15001, "rname": "好吃餐厅"}, {rno: 15002, "rname": "大众饭店"}, {rno: 15003, "rname": "国色天香"}],
        addAdminrnoRules: {
          aname: [{required: true, message: "名称不能为空", trigger: 'blur'},
            {max: 20, min: 2, message: '长度异常 长度在2-20之间', trigger: 'blur'}, {
              pattern: /^[\u0391-\uFFE5A-Za-z]+$/,
              message: '名称必须为中文',
              trigger: 'blur'
            }],
          aphone: [{required: true, message: "电话不能为空", trigger: 'blur'}, {
            max: 11,
            min: 11,
            message: '长度异常',
            trigger: 'blur'
          }, {pattern: /^\d+$|^\d+[.]?\d+$/, message: '必须为数字', trigger: 'blur'}],
        },
        //上传使用
        fileList: [[], [], []], //缓存区文件
        uploadFile: [[], [], []], //  上传用文件
        formData: {files: []},
        imagesUp: null,
        //上传使用结束

      }
    },
    methods: {
      addAdminrno: function (addAdminrnoForm) {
        this.$refs[addAdminrnoForm].validate(valid => {//此处refs对象和标签上的ref对应
          if (valid) {
            //如果通过验证 to do...
            //console.log(this.adminrno);
            this.setDataFromAxios();

            alert(this.adminrno.aname + "添加成功");
            this.handleClose();

          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      //页面初始化时加载的事件
      init() {
        this.addDialogIsShow = true;
      },
      //关闭页面
      handleClose() {
        this.addDialogIsShow = false;
        //给父组件传参
        this.$emit("addDialogIsShow", this.addDialogIsShow);
      },
      setDataFromAxios: function () {
        alert(this.$store.state.host);
        console.log(this.adminrno);
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios(this.$store.state.host + "/adminRnoController/addAdminRno", this.adminrno, "post")
          .then(res => {
            this.leagues = res.leagues
          });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
    },
    computed: {
      getUploadUrl() {
        return this.$store.state.host + "/uploadImg";
      }
    },
    //监听
    watch: {
      dialogisShow(newValue, oldValue) {
        this.addDialogIsShow = newValue;
      }
    }

  }
</script>

<style scoped>

</style>
